package javabasic.slipch.neiboringnumbers;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;

public final class Main {

	public static void main(String[] args) {
	
		final List<Integer> array = input();
		System.out.print("\nFirst element index of the nearest pair is: " +  getFirstIdxOfNearestPair(array));
	}
	
	private static int getFirstIdxOfNearestPair(final List<Integer> array) {
		assert array.size() >= 2;
		long minDist = (long)2 * (long)Integer.MAX_VALUE;
		int result = 0;
		for (int i = 1; i < array.size(); ++i) {
			long dist = Math.abs(array.get(i - 1) - array.get(i));
			if(dist < minDist) {
				result = i - 1;
				minDist = dist;
			}
		}
		return result;
	}
	
	private static List<Integer> input() {
		System.out.print("Enter an natural numbers array using \",\" ");
		Scanner inputReader = new Scanner(System.in);
		inputReader.useDelimiter("[,\\s]");
		List<Integer> result = new ArrayList<Integer>();
		while (true) {
				if ( inputReader.hasNextInt() ) {
					int number = inputReader.nextInt();
					result.add(  number );
				} else {
					String next = inputReader.next();
					if (next.isEmpty()){
						System.out.print("\nbreak\n");
						if( result.size() < 2 ) {
							System.out.print("\nInvalid input\n");
							System.out.print("\nEnter more than two numbers\n");
							result.clear();
							inputReader.nextLine();
						} else {
							break;
						}
					}
					System.out.print("\nInvalid input\n");
					System.out.print("\nShould be integers delimited with \",\"\n");
					System.out.print("\nTry again \n");
					
					result.clear();
					inputReader.nextLine();
					continue;
				}
		};
		return result;
	}
}
